# Make LaTeX chunk for Table 1

# Load Models and Store AUCs
countries <- c('indo', 'colo')
dvs <- c("any", "high", "spike")
algos <- c('lasso', 'rf', 'gbm', 
           'nn', 'ebma')

auc_array <- array(NA,
                   dim = c(length(countries),
                           length(dvs),
                           length(algos),
                           2),
                   dimnames = list(countries,
                                   dvs,
                                   algos,
                                   c('cv10', 'cv1')))


for (country in countries) {
  for (algo in algos) {
    modelname = algo
    for (dv in dvs) {
      filename <- paste(modeldir,"/",
                    country,
                    "_",algo,"_",
                    dv,
                    "_full.RData",
                    sep = "")
      load(filename)
      aucs_by_year = c()

      for (i in 1:length(get(paste(modelname,".results",sep="")))) {
          predictions <- as.vector(get(paste(modelname,".results",sep=""))[[i]]$fit.oos)
          realizations <- as.vector(get(paste(modelname,".results",sep=""))[[i]]$actual.oos)
          aucs_by_year <- c(aucs_by_year,
                            roc(response = realizations,
                                predictor = predictions)[['auc']])
      }
      auc_array[country, dv, algo, 'cv10'] <- mean(aucs_by_year)
      
      filename <- paste(modeldir,"/",
                        country,
                        "_",algo,"_",
                        dv,
                        "_full_1cv.RData",
                        sep = "")
      load(filename)
      aucs_by_year = c()
      
      for (i in 1:length(get(paste(modelname,".results",sep="")))) {
        predictions <- as.vector(get(paste(modelname,".results",sep=""))[[i]]$fit.oos)
        realizations <- as.vector(get(paste(modelname,".results",sep=""))[[i]]$actual.oos)
        aucs_by_year <- c(aucs_by_year,
                          roc(response = realizations,
                              predictor = predictions)[['auc']])
      }
      auc_array[country, dv, algo, 'cv1'] <- mean(aucs_by_year)
    }
  }
}


# Build Table with Labels
country.labels <- c(indo='Indonesia',
                    colo='Colombia')
outcome.labels <- c(any="(a) Any violent event",
                    high="(b) $\\ge$ 5 violent events",
                    spike="(c) $\\ge$ 1 s.d. increase in events")
model.labels <- c(lasso='LASSO',
                  rf='Random\\\\Forest',
                  gbm='Adaptive\\\\Boosting',
                  nn='Neural\\\\Network',
                  ebma='EBMA')
cv.labels <- c(cv10='10 CV Runs (Baseline)',
               cv1='1 CV Run')

table <- auc_array
dimnames(table)[[1]] <- country.labels[dimnames(table)[[1]]]
dimnames(table)[[2]] <- outcome.labels[dimnames(table)[[2]]]
dimnames(table)[[3]] <- model.labels[dimnames(table)[[3]]]
dimnames(table)[[4]] <- cv.labels[dimnames(table)[[4]]]

# Print to Latex
printB1(table = table,
                  filepath = "tables",
                  file = "table_B1")

